package com.haiertvbic.tvnow.protocol;

import java.io.IOException;
import java.nio.ByteBuffer;

import com.haiertvbic.tvnow.model.ChannelPackage;
import com.haiertvbic.tvnow.model.LocalCode;
import com.haiertvbic.tvnow.util.SLog;

public class CAEProtocol {
	// 得频道列表的返回码
	public static final int GET_FAVCHAS_SUC = 4006000;
	public static final int GET_FAVCHAS_SUC_2 = 4006001;
	public static final int NOT_SURE_ERROR = 4006004;
	public static final int DB_ERROR_1 = 4006002;
	public static final int DB_ERROR_2 = 4006003;

	private static final String TAG = "CAEProtocol";

	private static SocketPackage socketPackage = SocketPackage.getInstance();
	private static String ipString = ConnectManager.LOCAL_HOST;
	private static int port = ConnectManager.CHANNEL_PORT;

	/**
	 * 获取地域信息
	 * 
	 * @return 地域信息
	 */
	public static LocalCode getLocalCode() {
		LocalCode localInfo = null;
		if (socketPackage.createShortSocket(ipString, port)) {
			// 组合发送用的数据包
			ByteBuffer data = PackageOperation.groupDeviceInfoPackage();
			try {
				socketPackage.sendPacket(
						ConnectManager.CMD_CAE_APP_GET_LOCAL_CODE, 0, data);
				localInfo = socketPackage
						.getDeviceLocalInfo(ConnectManager.CMD_CAE_APP_GET_LOCAL_CODE_RE);
			} catch (IOException e) {
				SLog.i(TAG, e.toString());
			} catch (Exception e) {
				SLog.i(TAG, e.toString());
			} finally {
				if (socketPackage != null) {
					socketPackage.closeSocket();
				}
			}
		}
		return localInfo;
	}

	/**
	 * 返回当前的频道号，若没有取到则返回失败的返回码
	 * 
	 * @return
	 */
	public static int getCurrentChannel() {
		int channelId = -1;
		if (socketPackage.createShortSocket(ipString, port)) {
			// 组合发送用的数据包
			ByteBuffer data = PackageOperation.groupGetChannelIdPackage();
			try {
				socketPackage.sendPacket(ConnectManager.CMD_CAE_APP_GET_TV_ID,
						0, data);
				channelId = socketPackage
						.getTvCurrentChannelId(ConnectManager.CMD_CAE_APP_GET_TV_ID_RE);
			} catch (IOException e) {
				SLog.i(TAG, e.toString());
			} catch (Exception e) {
				SLog.i(TAG, e.toString());
			} finally {
				if (socketPackage != null) {
					socketPackage.closeSocket();
				}
			}
		}
		return channelId;

	}

	/**
	 * 电视换台的操作
	 * 
	 * @param channelId
	 *            换台频道的id
	 * @param channelNum
	 *            换成的频道号，扩充使用，可以为0
	 * @return 类型为int 0:读取换台信息失败 4004500:换台成功 4004501: 失败 ,此频道不存在 4004502:CRB
	 *         未插入，或者 CRB 故障 ， 提示用户插入或者重新插 拔 CRB 4004503 :未设置 CRB -2002连接断开
	 */
	public static int changeChannel(int channelId, int channelNum) {
		if (socketPackage.createShortSocket(ipString, port)) {
			// 电视设备换台
			int CONNECT_DEVICE_OTHER = 3;
			// 组合发送用的数据包
			ByteBuffer data = PackageOperation.groupChangeChannelPackage(
					CONNECT_DEVICE_OTHER, channelId, channelNum);
			try {
				socketPackage.sendPacket(ConnectManager.CMD_SENT_CHANNEL_INFO,
						0, data);
				return socketPackage
						.getChangeChannelSate(ConnectManager.CMD_SENT_CHANNEL_INFO_RE);
			} catch (IOException e) {
				SLog.i(TAG, e.toString());
			} catch (Exception e) {
				SLog.i(TAG, e.toString());
			} finally {
				if (socketPackage != null) {
					socketPackage.closeSocket();
				}
			}
		}
		return 0;
	}

	/**
	 * 获取喜爱频道列表 頻道和返回码标识的意思 4006000 成功 4006001 成功，但是数据库没有请求个数那么多的频道，正常情况很少出现
	 * 4006002 数据库不存在或丢失 4006003 操作数据库发生错误 4006004 未知错误，此种情况下UI可以重新请求
	 * 如果连接的ip有问题，会出现返回值为空。
	 * 
	 * @param type
	 *            查询类型 0：昨天的收视率 1：上周的收视率 2：上月的收视率
	 * @param num
	 *            查询的频道的数量
	 * @return 返回频道集合
	 * @throws Exception
	 */
	public static ChannelPackage getFavChannel(int type, int num) {
		ChannelPackage channelPagekage = null;
		if (socketPackage.createShortSocket(ipString, port)) {
			// 组合发送用的数据包
			ByteBuffer data = PackageOperation.groupTvChannelPackage(type, num,
					null);
			try {
				socketPackage.sendPacket(
						ConnectManager.CMD_CAE_APP_GET_AUDIO_RATE, 0, data);
				channelPagekage = socketPackage
						.getTvChannelPagekage(ConnectManager.CMD_CAE_APP_GET_AUDIO_RATE_RE);
			} catch (IOException e) {
				SLog.i(TAG, e.toString());
			} catch (Exception e) {
				SLog.i(TAG, e.toString());
			} finally {
				if (socketPackage != null) {
					socketPackage.closeSocket();
				}
			}
		}
		return channelPagekage;

	}
}
